package com.walker.rest;

import java.util.Map;

import javax.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.walker.service.meta.ExternalRestServ;
import com.walker.util.CommonUtils;

import net.sf.json.JSONObject;

@RestController
@RequestMapping("/rest/ds")
public class ExternalRestDSController {
	
	@Resource
	private ExternalRestServ serv;

	@SuppressWarnings("unchecked")
	@PostMapping("dsList")
	public String dsList(@RequestBody(required=false) String request) throws Exception {
		Map<String, Object> params = JSONObject.fromObject(request);
		JSONObject json = new JSONObject();
		try {
			if (!"".equals(CommonUtils.valueOf(params.get("project_id"))) && !"".equals(CommonUtils.valueOf(params.get("db_type")))) {
				json.put("data", serv.getDSList(params.get("project_id").toString(),params.get("db_type").toString()));
				json.put("flag", true);
				json.put("code", 200);
			} else {
				json.put("code", 500);
				json.put("msg", "project_id or ds_id not is null");
				return json.toString();
			}
		} catch (Exception e) {
			json.put("code", 500);
			json.put("msg", e.getMessage());
		}
		return json.toString();
	}
	
	@SuppressWarnings("unchecked")
	@PostMapping("dsParam")
	public String dsParam(@RequestBody String request) throws Exception {
		Map<String, Object> params = JSONObject.fromObject(request);
		JSONObject json = new JSONObject();
		try {
			if (!"".equals(CommonUtils.valueOf(params.get("ds_id")))) {
				json.put("data", serv.getDSParam(params.get("ds_id").toString()));
				json.put("flag", true);
				json.put("code", 200);
			} else {
				json.put("code", 500);
				json.put("msg", "ds_id not is null");
				return json.toString();
			}
		} catch (Exception e) {
			json.put("code", 500);
			json.put("msg", e.getMessage());
		}
		return json.toString();
	}
}
